{	Problem: http://codeforces.ru/problemset/problem/88/D
 	Verdict: Accepted
}
uses SysUtils, Math;

const
    vd='void';
    er='errtype';

type
    types = record
        n,t:string;
        kb:longint;
    end;
    
var 
    i,n,cc,tt,kk,ls,kb,inda,cn:longint;
    s,p,pa,pb:string;
    tp:array[0..1010] of string;
    pp:array[0..1010] of types;
    f:boolean;

procedure print(s:string; kb:longint);
var
    i:longint;
begin
write(s);
if s<>er then 
    for i:=1 to kb do 
        write('*');
writeln;
end;

function getFirst(s:string):string;
var
    i:longint;
begin
result:=s;
for i:=1 to cn do
    if pp[i].n=s then result:=getFirst(pp[i].t);
end;

function getB(s:string):longint;
var
    i:longint;
begin
result:=0;
for i:=1 to cn do
    if pp[i].n=s then result:=getB(pp[i].t)+pp[i].kb;
end;

procedure put(n,t:string; kb:longint);
var
    i,ind:longint;
begin
ind:=0;
for i:=1 to cn do
    if pp[i].n=n then begin
        ind:=i;
        break;
    end;
if ind=0 then begin
    inc(cn);
    ind:=cn;
end;
pp[ind].n:=n;
pp[ind].t:=getFirst(t);
if pp[ind].t=er then pp[ind].kb:=0 else pp[ind].kb:=getB(t)+kb;
if pp[ind].kb<0 then begin
    pp[ind].t:=er;
    pp[ind].kb:=0;
end;
end;

begin
readln(n);
cc:=2;
cn:=0;
tp[1]:='void';
tp[2]:='errtype';
for tt:=1 to n do begin
    readln(s);
    p:=copy(s,1,pos(' ',s)-1);
    delete(s,1,pos(' ',s));
    if p='typedef' then begin
        pa:=copy(s,1,pos(' ',s)-1);
        delete(s,1,pos(' ',s));
        pb:=s;
        f:=false;
        for i:=1 to cc do begin
            if tp[i]=pb then begin
                f:=true;
                break;
            end;
        end;
        if not f then begin
            inc(cc);
            tp[cc]:=pb;
        end;
        ls:=length(pa);
        kb:=0;
        for i:=1 to ls do
            if pa[i]='&' then dec(kb) else
            if pa[i]='*' then inc(kb);
        while pa[1]='&' do delete(pa,1,1);
        kk:=pos('*',pa);
        while kk>0 do begin
            delete(pa,kk,1);
            kk:=pos('*',pa);
        end;
        inda:=0;
        for i:=1 to cc do
            if tp[i]=pa then begin
                inda:=i;
                break;
            end;
        if inda>0 then put(pb,tp[inda],kb) else put(pb,er,0);
    end else begin
        pa:=s; 
        ls:=length(pa);
        kb:=0;
        for i:=1 to ls do
            if pa[i]='&' then dec(kb) else
            if pa[i]='*' then inc(kb);  
        while pa[1]='&' do delete(pa,1,1);
        kk:=pos('*',pa);
        while kk>0 do begin
            delete(pa,kk,1);
            kk:=pos('*',pa);
        end;
        f:=false;
        if (pa=vd) or (pa=er) then begin
            print(pa,kb);
            f:=true;
        end;
        for i:=1 to cc do
            if pp[i].n=pa then begin
                if pp[i].kb+kb<0 then print(er,0) else print(pp[i].t,pp[i].kb+kb);
                f:=true;
                break;
            end;
        if not f then writeln(er);
    end;
end;
end.